<?php

class CheckoutController extends FeController {

    public function init() {
        parent::init();
    }

    public function actionIndex() {
        $model = new CheckoutOrderForm();
        if (Yii::app()->user->getState('current_user') != NULL) {
            if (isset($_POST) && ($_POST != NULL)) {
                $removeArr = array_search("Remove", $_POST);
                if (isset($removeArr) && ($removeArr != NULL)) {
                    $removeArr = explode("_", $removeArr);
                    $removeItem = intval($removeArr[1]);
                    Yii::app()->shoppingCart->remove($removeItem);
                }
            }
            if (isset($_POST['submithome'])) {
                Yii::app()->request->redirect(SITE_PATH);
            }

            $amount = Yii::app()->shoppingCart->getCost();

            //get all subform actived on page checkout
            $frmCheckout = CheckoutForm::model()->findAll('t.`form_status`=:stt order by t.`form_weight` ASC', array(':stt' => 1));

            $this->layout = 'common.layouts.default.checkout';
            $this->render('index', array(
                'model' => $model,
                'amount' => $amount,
                'frmCheckout' => $frmCheckout,
            ));
        } else {
            Yii::app()->request->redirect(SITE_PATH_URL);
        }
    }

    public function actionNotify() {
        $orderID = (int) $_GET['id'];
        $order = Order::model()->findByPk($orderID);
        $this->layout = 'common.layouts.default.checkout';
        $this->render('notify', array('order' => $order));
    }

}
